How to Create a Simple Calculator in Android

Monday 7 December 2020

How to Create a Simple Calculator in Android

 In this tutorial, we are going to create a simple calculator in android by using android studio. This calculator has a very beautiful interface and user-friendly coding. First, you need to create a new project using android studio, then you need to give a specific name to your project. for example My Calculator. As you are given in the screenshots. 



Now you need to design the layout out your calculator by using the  XML language. XML coding is given below;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/edit_screen"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:textSize="20sp"
        android:padding="10dp"
        android:textAlignment="viewEnd"
        android:gravity="center"
        android:cursorVisible="false"
        android:paddingVertical="35dp"
        tools:ignore="RtlCompat">

    </EditText>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="25dp"
       android:paddingTop="1dp"
        >


        <Button
            android:id="@+id/btn_one"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="1"
            android:textSize="20sp"></Button>

        <Button
            android:id="@+id/btn_two"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="2"
            android:textSize="20sp"></Button>
        <Button
            android:id="@+id/btn_three"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="3"
            android:textSize="20sp"></Button>
        <Button
            android:id="@+id/btn_clear"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:textColor="@android:color/holo_red_light"
            android:textAllCaps="false"
            android:text="Clear"
            android:textSize="20sp"></Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="1dp"
        android:paddingTop="1dp"
        >


        <Button
            android:id="@+id/btn_four"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="4"
            android:textSize="20sp"></Button>

        <Button
            android:id="@+id/btn_five"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="5"
            android:textSize="20sp"></Button>
        <Button
            android:id="@+id/btn_six"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="6"
            android:textSize="20sp"></Button>
        <Button
            android:id="@+id/btn_power"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="@string/power"
            android:textSize="20sp"></Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="1dp"
        android:paddingTop="1dp"
        >


        <Button
            android:id="@+id/btn_seven"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="7"
            android:textSize="20sp"></Button>

        <Button
            android:id="@+id/btn_eight"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="8"
            android:textSize="20sp"></Button>
        <Button
            android:id="@+id/btn_nine"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="9"
            android:textSize="20sp"></Button>
        <Button
            android:id="@+id/btn_minus"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="-"
            android:textSize="20sp"></Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="1dp"
        android:paddingTop="1dp"
        >


        <Button
            android:id="@+id/btn_plus"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="+"
            android:textSize="20sp"></Button>

        <Button
            android:id="@+id/btn_zero"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="0"
            android:textSize="20sp"></Button>

        <Button
            android:id="@+id/btn_dot"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:background="@drawable/buttons"
            android:gravity="center"
            android:layout_margin="3dp"
            android:text="."
            android:textSize="20sp"></Button>
        <Button
            android:id="@+id/btn_divide"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:background="@drawable/buttons"
            android:gravity="center"
            android:layout_margin="3dp"
            android:text="/"
            android:textSize="20sp"></Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="1dp"
        android:paddingTop="1dp"
        >


        <Button
            android:id="@+id/btn_multiply"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/buttons"
            android:layout_margin="3dp"
            android:text="X"
            android:textSize="20sp"></Button>

        <Button
            android:id="@+id/btn_equal"
            android:layout_width="wrap_content"
            android:layout_height="73dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/equal_button"
            android:layout_margin="3dp"
            android:text= "="
            android:textSize="20sp"></Button>
    </LinearLayout>

</LinearLayout>


Here you are seen that android:background="@drawable/buttons" and android:background="@drawable/equal_button" in the XML code. I have made two drawable resource files with the name of  buttons and equal_button, creating these two you need to right on drawables and then follow the screenshots.



The code of  these two file are;


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="10dp"></corners>
           <solid android:color="#CFCACA"></solid>
        </shape>

    </item>

</selector>


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="10dp"></corners>
            <solid android:color="#31D152"></solid>
        </shape>

    </item>

</selector>


Java Coding: 

Now we are going to start java coding. This calculator performs the five mathematical operations like Addition, Subtraction, Multiplication, Division and Power of any number. Here is the java code;






  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.example.mycalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private Button btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine,btnZero,btnPlus,btnMinus,btnMultiply,
         btnDivide,btnEqual,btnClear,btnDot,btnPower;
private EditText editScreen;
private float firstNumber ,secondNumber,result;
private char operation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnOne = findViewById(R.id.btn_one);
        btnTwo = findViewById(R.id.btn_two);
        btnThree = findViewById(R.id.btn_three);
        btnFour = findViewById(R.id.btn_four);
        btnFive = findViewById(R.id.btn_five);
        btnSix = findViewById(R.id.btn_six);
        btnSeven = findViewById(R.id.btn_seven);
        btnEight = findViewById(R.id.btn_eight);
        btnNine = findViewById(R.id.btn_nine);
        btnZero = findViewById(R.id.btn_zero);
        btnMinus = findViewById(R.id.btn_minus);
        btnMultiply = findViewById(R.id.btn_multiply);
        btnPlus = findViewById(R.id.btn_plus);
        btnDivide = findViewById(R.id.btn_divide);
        btnEqual = findViewById(R.id.btn_equal);
        btnDot = findViewById(R.id.btn_dot);
        btnClear = findViewById(R.id.btn_clear);
        btnPower = findViewById(R.id.btn_power);
        editScreen = findViewById(R.id.edit_screen);

        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText("");
                firstNumber = (float) 0.0;
                secondNumber = (float) 0.0;
            }
        });

        btnOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnOne.getText().toString());
            }
        });

        btnTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnTwo.getText().toString());
            }
        });

        btnThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnThree.getText().toString());
            }
        });

        btnFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnFour.getText().toString());
            }
        });

        btnFive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnFive.getText().toString());
            }
        });

        btnSix.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnSix.getText().toString());
            }
        });

        btnSeven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnSeven.getText().toString());
            }
        });

        btnEight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnEight.getText().toString());
            }
        });

        btnNine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnNine.getText().toString());
            }
        });

        btnZero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText() + btnZero.getText().toString());
            }
        });

        btnPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editScreen.getText().toString().isEmpty())
                {
                    Toast.makeText(MainActivity.this, "Please! first write something...", Toast.LENGTH_SHORT).show();
                }
                else
                {
                   firstNumber = Float.parseFloat( editScreen.getText().toString());
                    editScreen.setText("");
                    operation = '+';
                }
            }
        });

        btnMultiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editScreen.getText().toString().isEmpty())
                {
                    Toast.makeText(MainActivity.this, "Please! first write something...", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    firstNumber = Float.parseFloat( editScreen.getText().toString());
                    editScreen.setText("");
                    operation = 'x';
                }
            }
        });

        btnMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editScreen.getText().toString().isEmpty())
                {
                    Toast.makeText(MainActivity.this, "Please! first write something...", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    firstNumber = Float.parseFloat( editScreen.getText().toString());
                    editScreen.setText("");
                    operation = '-';
                }
            }
        });

        btnDivide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editScreen.getText().toString().isEmpty())
                {
                    Toast.makeText(MainActivity.this, "Please! first write something...", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    firstNumber = Float.parseFloat( editScreen.getText().toString());
                    editScreen.setText("");
                    operation = '/';
                }
            }
        });

        btnDot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editScreen.setText(editScreen.getText().toString() + btnDot.getText().toString());
            }
        });
        btnPower.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editScreen.getText().toString().isEmpty())
                {
                    Toast.makeText(MainActivity.this, "Please! first write any number...", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    firstNumber = Float.parseFloat( editScreen.getText().toString());
                    result = firstNumber * firstNumber;
                    editScreen.setText(String.valueOf(result));
                }
            }
        });

        btnEqual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                if (editScreen.getText().toString().isEmpty())
                {
                    Toast.makeText(MainActivity.this, "Please first write something...", Toast.LENGTH_SHORT).show();
                }
                else
                    {
                    secondNumber = Float.parseFloat( editScreen.getText().toString());
                    switch (operation)
                    {
                        case '+':
                        {
                           addition(firstNumber,secondNumber);
                           break;
                        }
                        case '-':
                        {
                            substraction(firstNumber,secondNumber);
                            break;
                        }
                        case '/':
                        {
                            division(firstNumber,secondNumber);
                            break;
                        }
                        case 'x':
                        {
                           multiplication(firstNumber,secondNumber);
                            break;
                        }
                    }
                    editScreen.setText(String.valueOf(result));
                   }

            }
        });


    }

    private float substraction(float fNumber, float sNumber)
    {
        result = fNumber - sNumber;
        return result;
    }

    private float addition(float fNumber, float sNumber)
    {
        result = fNumber + sNumber;
        return result;
    }
    private float multiplication(float fNumber, float sNumber)
    {
        result = fNumber * sNumber;
        return result;
    }
    private float division(float fNumber, float sNumber)
    {
        result = fNumber / sNumber;
        return result;
    }
}

Now run the app through emulator or android device.

Output: Here are the screenshots of our Calculator

Making Android Calculator using android studio

I hope you liked it.

#AndroidDevelopment
#CalculatorInAndroid
#AndroidStudio
#Android
#Howwhy
#HowWhy308

Thanks





No comments:

Post a Comment